package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.domain.Commodity;
import com.example.demo.dao.CommodityDao;
import com.example.demo.domain.Supplier;
import com.example.demo.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-10-30
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityDao, Commodity> implements ICommodityService {

    @Override
    public IPage<Commodity> findCommodity(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        Page<Commodity> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Commodity> qw = new QueryWrapper<>();
        qw.like(condition.containsKey("name"), "p.name", condition.get("name"));
        qw.le(condition.containsKey("purchase_price"), "p.purchase_price", condition.get("purchase_price"));
        qw.ge(condition.containsKey("sale_price"), "p.sale_price", condition.get("sale_price"));
        qw.like(condition.containsKey("purchase_date"), "p.purchase_date", condition.get("purchase_date"));
        qw.like(condition.containsKey("sale_date"), "p.sale_date", condition.get("sale_date"));
        if(condition.containsKey("supplierName")){
            qw.eq("s.name", condition.get("supplierName"));
            this.baseMapper.findCommodityBySupplier(page, qw); //多表查询
        }else{
            this.baseMapper.findCommodity(page, qw); //单表查询
        }
        return page;
    }

    @Transactional
    @Override
    public Commodity addCommodity(Commodity commodity) {
        try {
            this.baseMapper.insert(commodity);
            for (Supplier supplier : commodity.getSupplierList()) {
                this.baseMapper.insertCommoditySupplier(commodity.getId(), supplier.getId());
            }
            return commodity;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return commodity;
    }

    @Transactional
    @Override
    public void updateCommodity(long id, Commodity commodity){
        if (this.baseMapper.selectById(id) == null) {
            //throw new ProductAdminException("产品" + id + "不存在");
            return;
        }
        try {
            this.baseMapper.updateById(commodity);
            this.baseMapper.deleteSupplierByCommodity(commodity.getId());
            for (Supplier supplier : commodity.getSupplierList()) {
                this.baseMapper.insertCommoditySupplier(commodity.getId(), supplier.getId());
            }
        } catch (Exception e) {
           // throw new ProductAdminException("修改产品失败：" + e.getMessage());
            e.printStackTrace();
        }


    }

}
